package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {

    private EditText etUsername, etPassword;
    private CheckBox cbRemember;
    private Button btnLogin, btnRegister;
    private UserDAO userDAO;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        // 初始化视图组件
        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);
        cbRemember = findViewById(R.id.cb_remember);
        btnLogin = findViewById(R.id.btn_login);
        btnRegister = findViewById(R.id.btn_register);

        userDAO = new UserDAO(this);

        // 检查是否有记住的用户
        checkRememberedUser();

        // 设置登录按钮点击事件
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });

        // 设置注册按钮点击事件
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }

    private void checkRememberedUser() {
        // 获取所有用户
        // 这里简化处理，假设只有一个用户会被记住
        User user = userDAO.getUserByUsername(etUsername.getText().toString());
        if (user != null && user.isRemember()) {
            etUsername.setText(user.getUsername());
            etPassword.setText(user.getPassword());
            cbRemember.setChecked(true);
        }
    }

    private void login() {
        String username = etUsername.getText().toString().trim();
        String password = etPassword.getText().toString().trim();

        if (username.isEmpty() || password.isEmpty()) {
            Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        // 验证用户
        User user = userDAO.getUserByUsername(username);
        if (user == null) {
            Toast.makeText(this, "用户不存在", Toast.LENGTH_SHORT).show();
            return;
        }

        if (!user.getPassword().equals(password)) {
            Toast.makeText(this, "密码错误", Toast.LENGTH_SHORT).show();
            return;
        }

        // 处理记住密码功能
        if (cbRemember.isChecked()) {
            user.setRemember(true);
        } else {
            user.setRemember(false);
        }
        DBHelper dbHelper = DBHelper.getInstance(LoginActivity.this);
        userDAO.updateUser(user, dbHelper);

        // 登录成功，跳转到主界面
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        intent.putExtra("userId", user.getId());
        startActivity(intent);
        finish();
    }
}
